import { PICK_APPLE, EAT_APPLE } from '@/actionTypes';

/**
 * apple 示例 Reducer
 * 
 * @param {any} state 
 * @param {any} action 
 */
let startId = 10;

function apples(state = [], action) {
  switch (action.type) {
    case PICK_APPLE:
      // id 累加
      startId += 1;
      return [
        ...state,
        {
          id: startId,
          name: '红苹果',
          weight: 250,
          isEaten: false,
        },
      ];
    case EAT_APPLE:
      return [
        ...state.slice(0, action.index),
        Object.assign({}, state[action.index], {
          isEaten: true,
        }),
        ...state.slice(action.index + 1),
      ];
    default:
      return state;
  }
}

export default apples;
